package com.tensquare.notice.controller;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.notice.pojo.Notice;
import com.tensquare.notice.pojo.NoticeFresh;
import com.tensquare.notice.service.NoticeService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 添加
     * @param notice
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Notice notice){
        // 调用业务添加
        noticeService.add(notice);
        // 返回操作结果
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * 通过id查询
     */
    @GetMapping("/{noticeId}")
    public Result findById(@PathVariable String noticeId){
        // 调用业务查询
        Notice notice = noticeService.findById(noticeId);
        return new Result(true, StatusCode.OK,"查询成功",notice);
    }

    /**
     * 分页查询
     */
    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size){
        // 调用业务分页查询
        PageResult<Notice> pageResult = noticeService.findPage(page,size);
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 修改通知
     */
    @PutMapping("/{noticeId}")
    public Result update(@PathVariable String noticeId, @RequestBody Notice notice){
        // 设置id
        notice.setId(noticeId);
        // 调用业务更新
        noticeService.update(notice);
        return new Result(true, StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{noticeId}")
    public Result deleteById(@PathVariable String noticeId){
        // 调用业务删除
        noticeService.deleteById(noticeId);
        return new Result(true, StatusCode.OK,"删除成功");

    }

    /**
     * 根据用户id查询该用户的待推送消息（新消息）
     * /notice/freshPage/{userId}/{page}/{size}
     */
    @GetMapping("/freshPage/{userId}/{page}/{size}")
    public Result freshPage(@PathVariable String userId, @PathVariable int page, @PathVariable int size){
        // 调用业务查询
        PageResult<NoticeFresh> pageResult = noticeService.freshPage(userId, page,size);
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }


}
